package vip.websky.project.user.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.UserRoleBO;
import vip.websky.project.module.entity.user.SysRole;
import vip.websky.project.module.entity.user.UserRole;
import vip.websky.project.user.dao.UserRoleDao;

import java.util.List;

/**
 * 用户-角色关联 服务类
 *
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/24-22:16
 **/
@Slf4j
@Service
public class UserRoleServer extends ServiceImpl<UserRoleDao, UserRole> {

    @Autowired
    private UserRoleDao userRoleDao;

    /**
     * 1.创建
     *
     * @param userRoleBOS 信息
     * @return boolean
     */
    @KingLog
    public boolean  insertUserRoleBatch(List<UserRoleBO> userRoleBOS) {
        List<UserRole> userRoles = ObjectConvertUtils.copyToList(userRoleBOS, UserRole.class);
        return super.insertBatch(userRoles);
    }

    /**
     * 2.根据条件查找一个
     *
     * @param userRoleBO 唯有条件
     * @return userRoleBO
     */
    @KingLog
    public UserRoleBO selectUserRole(UserRoleBO userRoleBO) {
        UserRole userRole = ObjectConvertUtils.copyToDest(userRoleBO, UserRole.class);
        userRole = userRoleDao.selectById(userRole.getId());
        return userRole != null ? ObjectConvertUtils.copyToDest(userRole, UserRoleBO.class):null;
    }

    /**
     * 3.获取角色列表
     *
     * @param userRoleBO 角色信息
     * @return Page<userRoleBO>
     */
    @KingLog
    public Page<UserRoleBO> selectUserRoleList(UserRoleBO userRoleBO) {
        Page<UserRoleBO> page = new Page<>(userRoleBO.getCurPage(), userRoleBO.getPageSize());
        EntityWrapper<SysRole> ew = new EntityWrapper<>();
        return page;
    }

    /**
     * 4.修改角色信息
     *
     * @param userRoleBOS 角色
     * @return boolean
     */
    @KingLog
    public boolean updateUserRoleBatch(List<UserRoleBO> userRoleBOS) {
        List<UserRole> userRoles = ObjectConvertUtils.copyToList(userRoleBOS, UserRole.class);
        return super.insertOrUpdateBatch(userRoles);
    }

    /**
     * 5.删除角色
     *
     * @param userRoleBOS id
     * @return boolean
     */
    @KingLog
    public boolean delUserRoleBatch(List<UserRoleBO> userRoleBOS) {
        List<UserRole> userRoles = ObjectConvertUtils.copyToList(userRoleBOS, UserRole.class);
        return super.deleteBatchIds(userRoles);
    }

    @KingLog
    public boolean delUserRoleByUserId(UserRoleBO userRoleBO) {
        UserRole userRole = ObjectConvertUtils.copyToDest(userRoleBO, UserRole.class);
        int result = userRoleDao.delete(
                new EntityWrapper<UserRole>().where("User_Id = {0}",userRole.getUserId()));
        return result > 0;
    }
}
